package controllers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import models.Device;
import models.Operator;
import models.Oplog;
import models.Repair;
import services.AuthorityService;
import services.DeviceService;
import services.RepairService;
import utils.BizConstants;
import utils.ExcelUtil;
import utils.FileConfig;
import vos.DeviceVo;
import vos.RepairSeachVo;
import vos.RepairVo;
import vos.ReturnCode;
import vos.ReturnListVo;

/**
 * 检修情况控制类
 * 
 * @ClassName: RepairController 
 * @author: wangsm
 * @date: 2016年6月3日 下午3:30:43
 */
public class RepairController extends BaseController {
	/**
	 * 检修情况页 
	 *
	 * @Title: repair 
	 * @return: void
	 */
	public static void repair(String menuid){
		Operator oper=getLoginUser();
		List<String> auths=AuthorityService.getAuthorityByOperAndMenu(menuid,oper.operatorid);
		for(String auth : auths){
			renderArgs.put(auth, auth);
		}
		render();
	}
	/**
	 * 获取检修列表
	 *
	 * @Title: getDealerLevel 
	 * @return: void
	 */
	public static void getRepair(int page,int rows, String sort,String order,String deviceid,String devicename){
		if(page==0){
			page=1;
		}
		if(rows==0){
			rows=BizConstants.pagesize;
		}
		RepairSeachVo vo=new RepairSeachVo(page,rows,sort,order,deviceid,devicename);
		long total=RepairService.countRepair(vo);
		ReturnListVo listVo=new ReturnListVo();
		listVo.setTotal(total);
		List<Repair> lists=RepairService.listRepair(vo);
		listVo.setRows(lists);
		renderJSON(listVo);
	}
	/**
	 * 批量删除
	 *
	 * @Title: betchEnable 
	 * @return: void
	 */
	public static void betchDel(String ids){
		if(!checkAuthority("REPAIR_BETCHDEL")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		if(StringUtils.isEmpty(ids)){
			renderFailedJson(ReturnCode.ErrorCode,"未选中记录");
		}
		boolean issuccess=RepairService.betchDel(ids);
		if(issuccess){
			Operator oper =getLoginUser();
			Oplog oplog=new Oplog(oper.operatorid,"批量删除检修"+ids,"检修管理");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode,"删除成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode,"删除失败");
		}
	}
	/**
	 * 删除记录
	 *
	 * @Title: delRepair 
	 * @param id 检修id
	 * @return: void
	 */
	public static void delRepair(String id){
		if(!checkAuthority("REPAIR_DEL")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		if(StringUtils.isEmpty(id)){
			renderFailedJson(ReturnCode.ErrorCode,"选中记录不能为空");
		}
		boolean issuccess=RepairService.delRepair(id);
		if(issuccess){
			Operator oper =getLoginUser();
			Oplog oplog=new Oplog(oper.operatorid,"删除检修"+id,"检修管理");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode,"删除成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode,"删除失败");
		}
	}
	/**
	 * 添加检修
	 *
	 * @Title: addRepair 
	 * @return: void
	 */
	public static void repairAdd(){
		render();
	}
	/**
	 * 复制检修
	 *
	 * @Title: addRepair 
	 * @return: void
	 */
	public static void repairCopy(Long id){
		RepairVo oldRepair=RepairService.getById(id);
		renderArgs.put("repair", oldRepair);
		render("RepairController/repairAdd.html");
	}
	/**
	 * 保存检修信息
	 *
	 * @Title: saveRepair 
	 * @param repair 检修对象
	 * @return: void
	 */
	public static void saveRepair(Repair repair){
		if(!checkAuthority("REPAIR_ADD")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		Operator oper=getLoginUser();
		if(repair==null){
			renderFailedJson(ReturnCode.ErrorCode, "检修不能为空");
		}
		if(StringUtils.isEmpty(repair.deviceid)){
			renderFailedJson(ReturnCode.ErrorCode, "设备不能为空");
		}
		if(StringUtils.isEmpty(repair.title)){
			renderFailedJson(ReturnCode.ErrorCode, "标题不能为空");
		}
		if(repair.title.length()>100){
			renderFailedJson(ReturnCode.ErrorCode, "标题长度不可大于100");
		}
		if(repair.remark.length()>255){
			renderFailedJson(ReturnCode.ErrorCode, "备注长度不能大于255");
		}
		Device device=DeviceService.getById(repair.deviceid);
		if(device==null){
			renderFailedJson(ReturnCode.ErrorCode, "设备不存在");
		}
		repair.createname=oper.operatorid;
		boolean isSuccess=RepairService.saveRepair(repair);
		if(isSuccess){
			Oplog oplog=new Oplog(oper.operatorid,"添加设备检修"+repair.title,"检修管理");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode, "保存成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode, "保存失败");
		}
	}
	/**
	 * 编辑检修
	 *
	 * @Title: repairEdit 
	 * @return: void
	 */
	public static void repairEdit(Long id){
		RepairVo oldRepair=RepairService.getById(id);
		renderArgs.put("repair", oldRepair);
		render();
	}
	/**
	 * 查看设备
	 *
	 * @Title: shopEdit 
	 * @return: void
	 */
	public static void repairView(Long id){
		if(id==null){
			renderFailedJson(ReturnCode.ErrorCode, "id不能为空");
		}
		RepairVo repair=RepairService.getById(id);
		renderArgs.put("repair", repair);
		render();
	}
	/**
	 * 保存检修信息
	 *
	 * @Title: saveRepair 
	 * @param repair 检修对象
	 * @return: void
	 */
	public static void updateRepair(Repair repair,Long repairid){
		if(!checkAuthority("REPAIR_EDIT")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		Operator oper=getLoginUser();
		if(repair==null){
			renderFailedJson(ReturnCode.ErrorCode, "检修不能为空");
		}
		if(repairid==null){
			renderFailedJson(ReturnCode.ErrorCode, "检修不存在");
		}
		repair.id=repairid;
		if(StringUtils.isEmpty(repair.title)){
			renderFailedJson(ReturnCode.ErrorCode, "标题不能为空");
		}
		if(repair.remark.length()>255){
			renderFailedJson(ReturnCode.ErrorCode, "备注长度不能大于255");
		}
		Device device=DeviceService.getById(repair.deviceid);
		if(device==null){
			renderFailedJson(ReturnCode.ErrorCode, "设备不存在");
		}
		repair.modifyname=oper.operatorid;
		boolean isSuccess=RepairService.updateRepair(repair);
		if(isSuccess){
			Oplog oplog=new Oplog(oper.operatorid,"编辑检修"+repair.title,"检修管理");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode, "保存成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode, "保存失败");
		}
	}
	/**
	 * 导出为excel
	 *
	 * @Title: exportXls 
	 * @param deviceid
	 * @param devicename
	 */
	public static void exportXls(String deviceid,String devicename){
		if(!checkAuthority("REPAIR_EXPORT")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		RepairSeachVo vo =new RepairSeachVo(deviceid,devicename);
		List<RepairVo> lists=RepairService.exportXls(vo);
		long y=System.currentTimeMillis();
		String fileName = "检修信息"+y+".csv";
        response.contentType = "application/x-download";
        Operator oper=getLoginUser();
        Oplog oplog=new Oplog(oper.operatorid,"导出检修信息数据","检修管理");
		oplog.save(oplog);
        renderBinary(ExcelUtil.buildExportFile(FileConfig.repairXlsTemplate, lists), fileName);
	}
}
